package com.example.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;


public class TransactionManagerUtil {

    private final static Logger log = LoggerFactory.getLogger(TransactionManagerUtil.class);

    public static  void afterCommitTransaction(Object object,Consumer<Object> consumer){

        if(TransactionSynchronizationManager.isActualTransactionActive()){
            TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
                @Override
                public void afterCommit(){
                    try {
                        consumer.accept(object);
                    }catch (Exception e){
                        log.info("事务异常，异常信息 = {}",e.getMessage());
                    }

                }

            });
        }else {
            log.info("当前请求不存在事务，业务代码不予执行");
        }
    }
}
